<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Page;
class AdminController extends CommonController {
    public function index(){
        $admin_object = D('admin');
        $map['status'] = array('egt', '0'); // 禁用和正常状态
        $Page = new Page($admin_object->where($map)->count(),C('ADMIN_CONFIG.page_size'));
        $Page->setConfig('prev','«');
        $Page->setConfig('next','»');
        $data_list     = $admin_object
            ->where($map)
            ->order('id asc')
            ->limit($Page->firstRow.','.$Page->listRows)
            ->select();
        $this->assign('mark','system');
        $this->assign('umark','admin');
        A('list')->setMetaTitle('管理员列表')
            ->setBreadCrumb(array('系统','系统权限','管理员列表'))
            ->addTopButton('add',array('title'=>'管理员添加'))
            ->addTopButton('resume')
            ->addTopButton('delete')
            ->addTableColumn('id','ID','number',50)
            ->addTableColumn('avatar','头像','avatar',50,'false')
            ->addTableColumn('account','账户名','text','auto','false')
            ->addTableColumn('status','状态','status',60)
            ->addTableColumn('ctime','创建时间','date',120)
            ->addTableColumn('','操作','rightbtn',200,'false')
            ->setTableDataList($data_list)
            ->setTableDataPage($Page->show())
            ->addRightButton('edit')
            ->addRightButton('forbid')
            ->addRightButton('delete')
            ->fetch();
    }
    //添加
    public function add(){
      $admin_object = D('admin');
      if (IS_POST) {
          $data = $admin_object->create();
          if ($data) {
              $id = $admin_object->add($data);
              if ($id) {
                  $ajax = array(
                    'status'=>1,
                    'text'=>'管理员新增成功',
                    'url'=>U('index')
                  );
              } else {
                  $ajax = array(
                    'status'=>0,
                    'text'=>'管理员新增失败',
                    'url'=>''
                  );
              }
          }else {
              $ajax = array(
                'status'=>0,
                'text'=>$link_object->getError(),
                'url'=>''
              );
          }
          $this->ajaxReturn($ajax);
      }else {
        $this->assign('mark','system');
        $this->assign('umark','admin');
        A('form')->setMetaTitle('管理员添加')
        ->setBreadCrumb(array('系统','系统权限','管理员列表','管理员添加'))
        ->setPostUrl(U('add'))
        ->addFormItem('avatar','image','头像')
        ->addFormItem('account','text','账户名称','',array(),array('required'=>true))
        ->addFormItem('password','password','账户密码','',array(),array('required'=>true))
        ->addFormItem('status','switch','状态','',array('启用','禁用'))
        ->setFormData(array('status'=>1,'sort'=>0))
        ->fetch();
      }
    }
    //编辑
    public function edit(){
      $admin_object = D('admin');
      if (IS_POST) {
          $data = $admin_object->create();
          if ($data) {
              $rs = $admin_object->save($data);
              if ($rs) {
                  $ajax = array(
                    'status'=>1,
                    'text'=>'管理员编辑成功',
                    'url'=>U('index')
                  );
              } else {
                  $ajax = array(
                    'status'=>0,
                    'text'=>'管理员编辑失败',
                    'url'=>''
                  );
              }
          }else {
              $ajax = array(
                'status'=>0,
                'text'=>$admin_object->getError(),
                'url'=>''
              );
          }
          $this->ajaxReturn($ajax);
      }else {
        $this->assign('mark','system');
        $this->assign('umark','admin');

        $id = I('get.ids',0,'intval');
        $form_data = $admin_object->find($id);
        A('form')->setMetaTitle('管理员编辑')
        ->setBreadCrumb(array('系统','系统权限','管理员列表','管理员编辑'))
        ->setPostUrl(U('edit'))
        ->addFormItem('id','hidden')
        ->addFormItem('avatar','image','头像')
        ->addFormItem('account','text','账户名称','',array(),array('required'=>true))
        ->addFormItem('password','password','账户密码','',array(),array('required'=>true))
        ->addFormItem('status','switch','状态','',array('启用','禁用'))
        ->setFormData($form_data)
        ->fetch();
      }
    }
    //状态修改
    public function setStatus(){
        if (IS_GET) {
          $ids = I('get.ids');
          $status = I('get.status');
          $admin_object = M('admin');
          $map = array(
            'id' => array('in',$ids),
          );
          $save = array(
            'status' => $status,
          );
          $rs = $admin_object->where($map)->save($save);
          if ($rs) {
              $ajax = array(
                'status'=>1,
                'text'=>'管理员状态修改成功',
                'url'=>U('index')
              );
          } else {
              $ajax = array(
                'status'=>0,
                'text'=>'管理员状态修改失败',
                'url'=>''
              );
          }
          $this->ajaxReturn($ajax);
        }
    }
    //删除
    public function delete(){
        if (IS_GET) {
          $ids = I('get.ids');
          $admin_object = M('admin');
          $map = array(
            'id' => array('in',$ids),
          );
          $rs = $admin_object->where($map)->delete();
          if ($rs) {
              $ajax = array(
                'status'=>1,
                'text'=>'管理员删除成功',
                'url'=>U('index')
              );
          } else {
              $ajax = array(
                'status'=>0,
                'text'=>'管理员删除失败',
                'url'=>''
              );
          }
          $this->ajaxReturn($ajax);
        }
    }
}
